package com.javaweb.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.javaweb.base.BaseService;
import com.javaweb.base.po.Interfaces;
import com.javaweb.constant.SystemConstant;
import com.javaweb.constant.SystemConstant.LogConstant;

import jakarta.servlet.http.HttpServletRequest;

/**
 * 操作日志记录切面
 * <p>
 * 操作日志记录常见的有URL正则匹配、数据库URL控制、Controller层注解标记等
 * @author tank2140896
 */
@Aspect
@ConditionalOnProperty(name="operatelog.aspect.open",havingValue="true",matchIfMissing=false)
@Component
public class OperateLogAspect extends BaseService {
	
	/**
	 * 环绕通知
	 * @param joinPoint 切面点
	 * @return Object object
	 * @throws Throwable Throwable
	 */
	@Around(value=LogConstant.DEFAULT_LOG_POINT_CUT)
	public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
		Object object = joinPoint.proceed();//注意该语句的顺序，在此处则只有操作成功的才会记录日志
		ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
		HttpServletRequest httpServletRequest = servletRequestAttributes.getRequest();
		Interfaces interfaces = (Interfaces)httpServletRequest.getAttribute(SystemConstant.REQUEST_URL_CHAIN_ATTRIBUTE_NAME);
		if(interfaces!=null&&interfaces.getLogRecord()!=null&&interfaces.getLogRecord()==1) {
			operationLogService.saveOperationLog(httpServletRequest,interfaces,joinPoint);
		}
		//Object object = joinPoint.proceed();//注意该语句的顺序，在此处则无论成功还是失败都会记录日志
		/**
		//获取Controller接口返回值
		if(object instanceof BaseResponseResult jsonInfo) {
			log.info("返回内容："+JSONObject.fromObject(object).toString());
		}else {
			System.out.println("不是BaseResponseResult对象");
		}
		*/
		return object;
	}
	
	/**
	@AfterReturning(value=SystemConstant.DEFAULT_LOG_POINT_CUT,returning="object")
	public void afterReturningMethod(Object object) {
		//do nothing
	}
	*/
	
}
